package com.recruit.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.recruit.mapper.DictMapper;
import com.recruit.mapper.UserMapper;
import com.recruit.pojo.Dict;
import com.recruit.service.UserService;
import com.recruit.pojo.User;
import com.recruit.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    @Autowired(required = false)
    private UserMapper userMapper;

    @Autowired(required = false)
    private DictMapper dictMapper;

    // 用户注册
    @Override
    public ResponseResult doReg(User user, String code, HttpSession session) {
        String code1 = (String) session.getAttribute("code");
        if (code1 == null) {
            return new ResponseResult(201, "验证码失效");
        }

        if (user.getEmail() == null || user.getEmail().equals("")) {
            return new ResponseResult(202, "请输入正确邮箱");
        }
/*        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email", user.getEmail());
        User user1 = userMapper.selectOne(queryWrapper);*/
        User userByEmail = userMapper.findUserByEmail(user.getEmail());
        if (userByEmail != null) {
            return new ResponseResult(203, "该邮箱已注册");
        }
        if (user.getPassword() == null || user.getPassword().equals("")) {
            return new ResponseResult(204, "请输入密码");
        }
        if (!code.equals(code1)) {
            return new ResponseResult(207, "验证码错误");
        }
        String uuid = UUID.randomUUID().toString();
        String md5Pwd = getMD5Pwd(user.getPassword(), uuid);
        user.setPassword(md5Pwd);
        user.setSalt(uuid);
        int row = userMapper.insert(user);
        if (row < 1) {
            return new ResponseResult(208, "注册失败");
        }
        return new ResponseResult(200, "注册成功");
    }

    // 用户登录
    @Override
    public ResponseResult doLogin(User user, HttpSession session) {

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email", user.getEmail());
        User user1 = userMapper.selectOne(queryWrapper);
        if (user1 == null) {
            return new ResponseResult(201,"该邮箱未注册");
        }

        String salt = user1.getSalt();
        String md5Pwd = getMD5Pwd(user.getPassword(), salt);
        if (!md5Pwd.equals(user1.getPassword())) {
            return new ResponseResult(202,"密码或者邮箱错误");
        }

        session.setAttribute("email", user1.getEmail());
        session.setAttribute("uid", user1.getEmail());
        return new ResponseResult(200,"登录成功");
    }

    // 用户信息界面返回数据
    @Override
    public String toUserInfo(HttpSession session, Model model) {
        String email = session.getAttribute("email").toString();
        if (email==null){
            model.addAttribute("msg","会话超时");
            return "before/user_login";
        }
        User user = userMapper.findUserByEmail(email);
        QueryWrapper<Dict> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("parent","86");
        List<Dict> province = dictMapper.selectList(queryWrapper);
        if (user.getProvince()!=null && !user.getProvince().equals("")){
            queryWrapper.eq("name",user.getProvince());
            Dict dict = dictMapper.selectOne(queryWrapper);
            QueryWrapper<Dict> wrapper =new QueryWrapper<>();
            wrapper.eq("parent",dict.getCode());
            List<Dict> city = dictMapper.selectList(wrapper);
            model.addAttribute("city",city);
        }
        model.addAttribute("user",user);
        model.addAttribute("province",province);
        return "before/user_info";
    }

    @Override
    public ResponseResult findCityByParent(String parent) {
        QueryWrapper<Dict> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("parent",parent);
        List<Dict> city = dictMapper.selectList(queryWrapper);

        return new ResponseResult(200,"查找成功",city);
    }

    @Override
    public ResponseResult updataUserInfo(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email",user.getEmail());
        User user1 = userMapper.selectOne(queryWrapper);
        if (!user.getPassword().equals(user1.getPassword())){
            String salt = UUID.randomUUID().toString();
            String md5Pwd = getMD5Pwd(user.getPassword(), salt);
            user.setPassword(md5Pwd);
            user.setSalt(salt);
        }
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("code",user.getProvince());
        Dict dict = dictMapper.selectOne(wrapper);
        user.setProvince(dict.getName());
        QueryWrapper<Dict> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("code",user.getCity());
        Dict dict1 = dictMapper.selectOne(queryWrapper1);
        user.setCity(dict.getName());
        int row = userMapper.update(user, queryWrapper);
        if (row<1){
            return new ResponseResult(202,"保存失败");
        }
        return new ResponseResult(200,"保存成功");
    }

    public String getMD5Pwd(String password, String salt) {
        String md5Pwd = password + salt;
        for (int i = 0; i < 10; i++) {
            md5Pwd = DigestUtils.md5DigestAsHex(md5Pwd.getBytes());
        }
        return md5Pwd;
    }
}
